package com.qdxwx.data.service.user;

import com.qdxwx.common.page.PageRequest;
import com.qdxwx.data.mapper.user.UserAddressMapper;
import com.qdxwx.models.user.UserAddress;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * Qdx NetSchool Platform
 * @author qdxwx
 * @updateTime 2022/5/25
 * @description
 */
@Service
public class UserAddressServiceImpl implements UserAddressService {

    @Autowired
    private UserAddressMapper userAddressMapper;

    @Override
    public UserAddress get(int id) {
        if (id < 1) return null;
        return userAddressMapper.selectById(id);
    }

    @Override
    public int insert(UserAddress userAddress) {
        userAddress.setCreateTime(LocalDateTime.now());
        return userAddressMapper.insertUserAddress(userAddress);
    }

    @Override
    public int update(UserAddress userAddress) {
        if (userAddress.getId() < 1) return 0;
        return userAddressMapper.updateUserAddress(userAddress);
    }

    @Override
    public int delete(int id) {
        if (id < 1) return 0;
        return userAddressMapper.deleteById(id);
    }

    @Override
    public List<UserAddress> getList(UserAddress userAddress, PageRequest page) {
        return userAddressMapper.getList(userAddress, page);
    }

    @Override
    public List<UserAddress> getList(long userId, int limit) {
        if (userId < 1) return null;
        UserAddress userAddress = new UserAddress();
        userAddress.setUserId(userId);
        return userAddressMapper.getList(userAddress, new PageRequest(1, 3));
    }

    @Override
    public long count(UserAddress userAddress) {
        return userAddressMapper.count(userAddress);
    }

    @Override
    public int updateCombine(long userId1, long userId2) {
        if(userId1 < 1 || userId2 < 1){
            throw new RuntimeException("更新收货地址：参数错误");
        }
        return userAddressMapper.updateCombine(userId1, userId2);
    }
}
